﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace wf6_8tightDisp
{
    public partial class Form1 : Form
    {

        int i = -1; //case of display
        delegate void MyDlgt(string s); //define delegate type
        readonly MyDlgt[] _delegates = new MyDlgt[4]; //array of delegate objects 

        public Form1()
        {
            InitializeComponent();
            RFuTFuD.Checked = false;//not necessary if set to false at design time
            RFuTFuD.Checked = true; //force population of delegates
        }

        void FuT(string s)
        {
            var rex = new Regex("\\s+");
            TxBTight.Text = rex.Replace(s, "");
        }

        void FuD(string s)
        {
            var rex = new Regex("\\s+");
            var ss = rex.Replace(s, ""); //use of instance Replace
            rex = new Regex("");
            TxBDispersed.Text = rex.Replace(ss, " ");
        }

        void FuTWipe(string s)
        {
            FuT("");
            //TxBTight.Text = String.Empty;
        }

        void FuDWipe(string s)
        {
            FuD("");
        }

        private void RFuTFuD_CheckedChanged(object sender, EventArgs e)
        {
            _delegates[0] = FuT;
            _delegates[1] = FuD;
            _delegates[2] = FuTWipe;
            _delegates[3] = FuDWipe;
        }

        private void RAnony_CheckedChanged(object sender, EventArgs e)
        {
            _delegates[0] = delegate(string s) { TxBTight.Text = Regex.Replace(s, "\\s+", "") + " *"; };
            _delegates[1] = delegate(string s) { TxBDispersed.Text = Regex.Replace(Regex.Replace(s, "\\s+", ""), "\\s?", " ") + " *"; };
            _delegates[2] = delegate(string s) { TxBTight.Text = ""; };      //above: use of static Replace 
            _delegates[3] = delegate(string s) { TxBDispersed.Text = ""; };
        }

        private void NuUpD_ValueChanged(object sender, EventArgs e)
        {
            int n = (int)NuUpD.Value;
            TxBInput.MaxLength = n;
            if (TxBInput.Text.Length > n) TxBInput.Text = TxBInput.Text.Remove(n); 
        }

        private void TxBInput_TextChanged(object sender, EventArgs e)
        {
            NuUpD_ValueChanged(this, EventArgs.Empty);
        }

        private void BNext_MouseDown(object sender, MouseEventArgs e)
        {
            string s = TxBInput.Text.ToUpper();
            if (s.Length <= 0)
            {
                MessageBox.Show("No text in input");
                return;
            }
            i = ++i % 4;
            _delegates[i](s);
        }
    }
}
